package com.example.finance_manager.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.example.finance_manager.R;
import com.example.finance_manager.pages.manage.IncomeManageActivity;
import com.example.finance_manager.models.Salary;

import java.util.List;
import com.example.finance_manager.pages.my.MyIncomeActivity;

public class IncomeAdapter extends BaseAdapter {
    private final List<Salary> mList;
    private final LayoutInflater mInflater;
    private final Intent intent;
    private final Context mContext;

    public IncomeAdapter(Context mContext, List<Salary> mList) {
        this.mList = mList;
        this.mInflater = LayoutInflater.from(mContext);
        this.intent = new Intent(mContext, IncomeManageActivity.class);
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList == null ? null : mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup parent) {
        View item;
        if (convertView == null) {
            item = mInflater.inflate(R.layout.item_my_income, parent, false);
        } else {
            item = convertView;
        }
        item.setOnClickListener(view -> {
            Salary salary = mList.get(i);
            Bundle bundle = new Bundle();
            bundle.putSerializable("salary", salary);
            intent.putExtras(bundle);
            mContext.startActivity(intent);
            MyIncomeActivity activity = (MyIncomeActivity) mContext;
            activity.finish();
        });
        TextView tvTime = item.findViewById(R.id.tv_item_income_time);
        TextView tvType = item.findViewById(R.id.tv_item_income_type);
        TextView tvPay = item.findViewById(R.id.tv_item_income_pay);
        TextView tvOther = item.findViewById(R.id.tv_item_income_other);
        TextView tvMoney = item.findViewById(R.id.tv_item_income_money);
        Salary salary = mList.get(i);
        tvTime.setText(salary.getTime());
        tvMoney.setText(salary.getMoney().toString());
        tvType.setText(salary.getType());
        tvPay.setText(salary.getPay());
        tvOther.setText(salary.getOther());
        return item;
    }
}
